package com.sodaopen.crm.customer.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sodaopen.crm.customer.entity.Customer;
import com.sodaopen.crm.customer.entity.Record;
import com.sodaopen.crm.customer.mapper.CustomerMapper;
import com.sodaopen.crm.customer.service.ICustomerService;
import com.sodaopen.crm.customer.service.IRecordService;
import com.sodaopen.crm.customer.vo.CustomerVO;
import com.sodaopen.crm.customer.vo.RecordVO;
import com.sodaopen.crm.sys.entity.User;
import com.sodaopen.crm.sys.service.IUserService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SodaJava
 * @since 2020-07-04
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    private IRecordService recordService;

    @Autowired
    private IUserService userService;

    @Override
    public CustomerVO getCustomerByIdWithRecords(Integer id) {
        Customer customer = this.getById(id);
        if(customer == null){
            return null;
        }

        QueryWrapper<Record> query = new QueryWrapper<>();
        query.eq("customer_id", id);
        query.orderByDesc("create_date");
        List<Record> records = recordService.list(query);

        List<RecordVO> recordVOList = new ArrayList<>();
        for(Record r : records){
            User user = userService.getById(r.getCreateBy());
            RecordVO rvo = new RecordVO();
            BeanUtils.copyProperties(r, rvo);
            rvo.setCreateByName(user.getName());
            recordVOList.add(rvo);
        }


        CustomerVO cvo = new CustomerVO();
        BeanUtils.copyProperties(customer, cvo);
        cvo.setRecords(recordVOList);

        return cvo;
    }
    
    
    /**
     * 查询所有国家的唯一值
     */
	@Override
	public List<String> distinctCountry() {
		QueryWrapper<Customer> query = new QueryWrapper<>();
    	query.select("distinct country");
    	query.isNotNull("country");
    	query.ne("country", "");
        List<Customer> list = this.list(query);

        List<String> result = new ArrayList<>();
        for(Customer cus : list) {
        	result.add(cus.getCountry());
        }
        
		return result;
	}

    @Override
    public Page<Customer> queryCustomer(Map<String, Object> params, Integer page, Integer limit) {
        QueryWrapper<Customer> query = new QueryWrapper<>();

        query.like(!StringUtils.isEmpty(params.get("company")), "company", params.get("company"));
        query.like(!StringUtils.isEmpty(params.get("name")), "name", params.get("name"));
        query.like(!StringUtils.isEmpty(params.get("email")), "email", params.get("email"));
        query.like(!StringUtils.isEmpty(params.get("mobile")), "mobile", params.get("mobile"));
        query.eq(!StringUtils.isEmpty(params.get("country")), "country", params.get("country"));
        query.eq(!StringUtils.isEmpty(params.get("star")), "star", params.get("star"));
        query.orderBy(!StringUtils.isEmpty(params.get("orderColumn")),
                (StringUtils.isEmpty(params.get("order")) || params.get("order").equals("ascending")),
                StrUtil.toUnderlineCase((String)params.get("orderColumn")));

        return page(new Page<>(page, limit), query);
    }

    @Override
    @Transactional
    public void addCustomer(Customer customer) {
	    customer.setLastFollow(LocalDateTime.now());
	    save(customer);

	    Record record = new Record();
	    record.setContent("<创建客户信息>");
	    record.setCreateBy(customer.getFollower());
	    record.setCustomerId(customer.getId());
	    record.setCreateDate(LocalDateTime.now());
        recordService.save(record);
    }
}
